package jdbc;

import jdbc.common.InputField;
import jdbc.common.InputUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class LoginUserDemo {
    public static void main(String[] args) {
        try (Connection conn = DBUtil.getConnection();){
            Statement stat = conn.createStatement();
            //UserInfo user = InputUtil.getInputObject(new UserInfo(),"用户登录");
            Scanner scan = new Scanner(System.in);
            System.out.println("请输入用户名：");
            String username = scan.nextLine();
            String sql = "SELECT id,username,password,nickname,age \n" +
                    "FROM userinfo\n" +
                    "WHERE username = '"+username+"'";
            ResultSet rs = stat.executeQuery(sql);
            if (rs.next()) {
                System.out.println("请输入用户密码：");
                String password = scan.nextLine();
                sql = "SELECT id,username,password,nickname,age \n" +
                        "FROM userinfo\n" +
                        "WHERE username = '"+username+"'AND password = '" + password + "'";
                rs = stat.executeQuery(sql);
                if (rs.next()) {
                    System.out.println("登录成功");
                } else {
                    System.out.println("密码错误");
                }
            }else{
                System.out.println("用户不存在");
            }



        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
